package com.pro.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Random;

@Service
public class PubServiceImpl implements PubService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 发送验证码（生成四位随机数，并发送到redis，定时，再发送到rabbitmq中）
    @Override
    public Integer SendYanzhengma() {
        int yanzhengma = new Random().nextInt(9999);
        /*把验证码发送到redis*/
        stringRedisTemplate.opsForValue().set("yanzhengma", String.valueOf(yanzhengma), Duration.ofHours(1));
        /*将验证码发送到rabbitmq*/
        String queueName="pub-yanzhengma";
        Integer msg = yanzhengma;
        rabbitTemplate.convertAndSend(queueName,msg);
        return yanzhengma;
    }
}
